package me.zhengjie.modules.utils;

import me.zhengjie.config.AppConfig;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogUtils {
    static BufferedWriter writer;

    public static void writeToFile(String data) {
        if (!AppConfig.isDebug) {
            return;
        }

        System.out.println(data);

        try {
            if (writer == null) {
                writer = new BufferedWriter(new FileWriter(AppConfig.logFile, true));
            }

            writer.write(getTime() + "  " + data);
            writer.newLine();
            writer.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getTime() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }
}
